import { request } from "../../request/index.js";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs:[
    {
      id:0,
      title:"综合",
      isChose:true
    },
    {
      id:1,
      title:"价格",
      isChose:false
    },
    {
      id:2,
      title:"销量",
      isChose:false
    }
  ],
  goodsList:[]
  },
//传的数据
  queryParms:{
    query:"",
    cid:"",
    pagenum:1,
    pagesize:10
  },
  //总条数
  pageTotal:0,
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.queryParms.cid =options.cid;
    wx.startPullDownRefresh();
  },
  // 上拉加载
  onReachBottom(){
    if(this.queryParms.pagenum>=this.pageTotal){
      console.log("没有下一页");
      wx.showToast({
        title: "没有数据了",
        duration: 2000,
        icon:'none',
        mask: true,
      })
    }else{
      console.log("有下一页");
      this.queryParms.pagenum++
      this.getGoodsList(); 
    }
    console.log(this.queryParms.pagenum+"----"+this.pageTotal);
  },
  //下拉刷新
  onPullDownRefresh(){
    this.queryParms.pagenum=1;
    this.setData({
      goodsList:[],
    })
    this.getGoodsList(); 
  },

  handleTagsItemChange(e){
    console.log(e);
    const {index} =e.detail;
    let {tabs} =this.data;
    tabs.forEach((v,i)=>i===index?v.isChose=true:v.isChose=false);
    this.setData({
      tabs
    })
  },
  getGoodsList(){
    request({url:'https://api-hmugo-web.itheima.net/api/public/v1/goods/search',data:this.queryParms})
    .then(result=>{
      // 获取总条数
      const total =result.data.message.total;
      this.pageTotal =Math.ceil(total/this.queryParms.pagesize);
      console.log(this.pageTotal);
      if(this.queryParms.pagenum==1){
        wx.stopPullDownRefresh();
      }
       this.setData({
        goodsList:[...this.data.goodsList,...result.data.message.goods]
       })
    })
  }
})